var express = require('express');
var axios = require('axios');
var router = express.Router();

router.get('/', function (req, res, next) {
    // res.send("登陆页面");
    res.render('login/login');
})

/**
 * @api {post} /login/login 管理员登录
 * @apiName login
 * @apiGroup admin
 *
 * @apiParam {string} user_number 账号.
 * @apiParam {string} password 密码.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/login', (req, res, next) => {
    console.log("admin")
    let { user_number, password } = req.body
    var config = {
        method: 'post',
        baseURL: 'http://localhost:9023',
        url: '/Teacher/Login',
        params: {
            user_number: user_number,
            password: password,
            kind: 'a'
        }
    }
    axios(config)
        .then(function (response) {
            // console.log(response.data);
            if (response.data.code == 0) {
                console.log("登录成功")
                req.session.login = true
                req.session.identity = "admin"
                req.session.userId = response.data.data.id
                req.session.userName = response.data.data.userName
                req.session.userNumber = response.data.data.userNumber
                req.session.save()
                res.send({ err: 0, msg: "登录成功", data: { url: '/admin/launchExam' } })
            } else {
                res.send({ err: -1, msg: "登录失败" })
                console.log("登录失败")
            }
        })
        .catch(function (error) {
            // console.log(error);
            res.send({ err: -1, msg: "服务器错误" })
            console.log("登录失败")
        });
})

/**
 * @api {post} /login/tealogin 教师登录
 * @apiName login
 * @apiGroup admin
 *
 * @apiParam {string} user_number 账号.
 * @apiParam {string} password 密码.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/tealogin', (req, res, next) => {
    console.log("laoshi")
    let { user_number, password } = req.body
    var config = {
        method: 'post',
        baseURL: 'http://localhost:9023',
        url: '/Teacher/Login',
        params: {
            user_number: user_number,
            password: password,
            kind: 'b'
        }
    }
    axios(config)
        .then(function (response) {
            // console.log(response.data);
            if (response.data.code == 0) {
                console.log("登录成功")
                res.send({ err: 0, msg: "登录成功", data: { url: '/admin/addpagelist' } })
                req.session.login = true
                req.session.identity = "tea"
                req.session.userId = response.data.data.id
                req.session.userName = response.data.data.userName
                req.session.userNumber = response.data.data.userNumber
                req.session.save()
                console.log(req.session.userName)
            } else {
                res.send({ err: -1, msg: "登录失败" })
                console.log("登录失败")
            }
        })
        .catch(function (error) {
            // console.log(error);
            res.send({ err: -1, msg: "服务器错误" })
            console.log("服务器错误")
        });
})

/**
 * @api {post} /login/stulogin 考生登录
 * @apiName login
 * @apiGroup admin
 *
 * @apiParam {string} user_number 账号.
 * @apiParam {string} password 密码.
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/stulogin', (req, res, next) => {
    console.log("xueshegn")
    let { user_number, password } = req.body
    var config = {
        method: 'post',
        baseURL: 'http://localhost:9023',
        url: '/student_work',
        params: {
            idNumber: user_number,
            password: password,
        }
    }
    axios(config)
        .then(function (response) {
            console.log(response.data);
            if (response.data.code == 0) {
                console.log("登录成功")
                res.send({ err: 0, msg: "登录成功", data: { url: '/admin/index' } })
                req.session.login = true
                req.session.identity = "stu"
                req.session.userId = response.data.data.id
                req.session.testNumber = JSON.stringify(response.data.data.testNumber)
                req.session.testNumberRel = response.data.data.testNumber
                req.session.userName = response.data.data.userName
                req.session.userNumber = response.data.data.userNumber
                console.log(req.session)
                req.session.save()
            } else {
                res.send({ err: -1, msg: "登录失败" })
                console.log("登录失败")
            }
        })
        .catch(function (error) {
            // console.log(error);
            res.send({ err: -1, msg: "服务器错误" })
            console.log("服务器错误")
        });
})

/**
 * @api {post} /admin/login/logout 安全登出
 * @apiName login
 * @apiGroup admin
 *
 *
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/logout',(req, res, next) => {
    if (req.session.login) {
        next()
    } else {
        res.send({ err: -999, msg: '请先登录' })
    }
}, (req, res, next) => {
    req.session.destroy()   // 销毁保存的 session
    res.send({ err: 0, msg: '已退出' })
})


/**
 * @api {post} /admin/login/public 渲染公共部分
 * @apiName public
 * @apiGroup admin
 *
 *
 * @apiSuccess {String} firstname Firstname of the User.
 * @apiSuccess {String} lastname  Lastname of the User.
 */
router.post('/public', (req, res, next) => {
    var user ={
        userName:req.session.userName,
    }
    res.send({ err: 0, msg: '',data:req.session.userName })
})


module.exports = router;
